package beans;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import model.AdminMessage;
import model.GeneralMessage;

import conversions.Conversions;
import dao.MailDao;
import dao.UserDao;
import dto.AdminMessageDTO;
import dto.GeneralMessageDTO;

/**
 * Session Bean implementation class InboxBean
 */
@Stateless
public class InboxBean implements InboxBeanRemote {

	/**
	 * Default constructor.
	 */
	@EJB
	Conversions conv;

	@EJB
	MailDao mDao;

	@EJB
	UserDao uDao;

	public InboxBean() {

		// TODO Auto-generated constructor stub
	}

	/**
	 * @author zirbom
	 * @return dtoMailList
	 * 
	 *         metoda ia lista de mail-uri din baza de date, face conversia
	 *         intr-o Lista de General
	 */
	@Override
	public List<GeneralMessageDTO> getMailList() {

		List<AdminMessage> mailList = mDao.getAdminMailList();
		List<GeneralMessageDTO> dtoMailList = new ArrayList<GeneralMessageDTO>();

		for (AdminMessage mL : mailList) {

			GeneralMessageDTO newMailDTO = new GeneralMessageDTO();
			newMailDTO = conv.adminMessageModelTODto(mL);
			dtoMailList.add(newMailDTO);

		}

		return dtoMailList;
	}

	/**
	 * adauga un admin message
	 * 
	 * @param adminMessageDTO
	 * @author fodorl
	 */

	@Override
	public boolean addAdminMessage(AdminMessageDTO adminMessageDTO) {

		boolean ok = false;
		AdminMessage adminMessage = new AdminMessage();
		adminMessage = conv.adminMessageFromDTO(adminMessageDTO);
		ok = mDao.existaAdminMessage(adminMessage);
		if (ok) {
			return false;
		} else {
			mDao.insertAdminMessage(adminMessage);
			return true;
		}

	}

	/**
	 * adauga un general Message
	 * 
	 * @param generalMessageDTO
	 * @author fodorl
	 */

	@Override
	public boolean addGeneralMessage(GeneralMessageDTO generalMessageDTO) {

		boolean ok = false;
		GeneralMessage generalMessage = new GeneralMessage();
		generalMessage = conv.generalMessageFromDTO(generalMessageDTO);
		ok = mDao.existaGeneralMessage(generalMessage);
		if (ok) {
			return false;
		} else {
			mDao.insertGeneralMessage(generalMessage);
			return true;
		}

	}

	/**
	 * @author zirbom
	 */
	@Override
	public List<GeneralMessageDTO> getMailList(int id) {

		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @author zirbom returneaza lista de Mail pentru Admin User
	 * 
	 * */
	public List<GeneralMessageDTO> getAdminMailList() {

		List<GeneralMessageDTO> adminEmails = new ArrayList<GeneralMessageDTO>();
		List<AdminMessage> mAdminEmails = mDao.getAdminMailList();

		for (AdminMessage message : mAdminEmails) {

			GeneralMessageDTO mail = new GeneralMessageDTO();
			mail = conv.adminMessageModelTODto(message);
			adminEmails.add(mail);
		}

		return adminEmails;
	}

	/**
	 * @author zirbom returneaza Lista de Mail-uri pentru supervisor
	 */

	@Override
	public List<GeneralMessageDTO> getUserMailList(int idUser, int idRepository) {

		List<GeneralMessageDTO> supervisorEmails = new ArrayList<GeneralMessageDTO>();
		List<GeneralMessage> mGeneralMessage = new ArrayList<GeneralMessage>();
		
		List<AdminMessage> cRepMes = new ArrayList<AdminMessage>();
		List<GeneralMessageDTO> subList = new ArrayList<GeneralMessageDTO>();

		if (!uDao.isSupervisor(idUser, idRepository)) {

			try {
				mGeneralMessage = mDao.getStatusRequests(idUser, idRepository);
				
				
				cRepMes = mDao.getForAdminEntries(idUser,idRepository);
				
				
				
				for (AdminMessage mail : cRepMes) {

					GeneralMessageDTO gMail = new GeneralMessageDTO();
					gMail = conv.adminMessageModelTODto(mail);

					subList.add(gMail);

				}
				
				
			} catch (SQLException e) {
				e.printStackTrace();
			}

		} else {

			try {
				mGeneralMessage = mDao.getUserMailList(idUser, idRepository);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		for (GeneralMessage mail : mGeneralMessage) {

			GeneralMessageDTO gMail = new GeneralMessageDTO();
			gMail = conv.generalMessageModelTODto(mail);

			supervisorEmails.add(gMail);

		}

		supervisorEmails.addAll(subList);
		// TODO Auto-generated method stub
		return supervisorEmails;
	}

	@Override
	public void deleteEntry(int id) {

		mDao.deleteGMEntry(id);
		mDao.deleteAMEntry(id);

	}

}
